//生成任意区间的随机整数
function randomInt(min, max) {
    var temp;
    if (max < min) {
        temp = max;
        max = min;
        min = temp;
    }
    return Math.round(Math.random() * (max - min)) + min;
}

//随机生成位数随机(4~8)的验证码
//min,max为要生成的验证码位数
function randomCode(min, max) {
    var temp;
    var len;
    if (max == undefined) {
        len = min;
    } else {
        if (max < min) {
            temp = max;
            max = min;
            min = temp;
        }
        len = randomInt(min, max);
    }
    var arr = [];
    arr.length = len;
    for (var key = 0; key < arr.length; key++) {
        do {
            var ascii = randomInt(48, 122);
        } while ((ascii > 57 && ascii < 65) || (ascii > 90 && ascii < 97))
        arr[key] = String.fromCharCode(ascii);
    }
    return arr.join("");
}

//生成随机的十六进制颜色值 #
function randomColor() {
    var str = "0123456789abcdef";
    var col = "#";
    for (var i = 0; i < 6; i++) {
        var index = randomInt(0, 15);
        col += str[index];
    }
    return col;
}

//生成随机的十六进制颜色值 #
// function randomColor() {
//     var str = "#";
//     for (var i = 0; i < 6; i++) {
//         str += parseInt(Math.random() * 16).toString(16)
//     }
//     return str;
// }

//封装getbyclassname 解决兼容问题
function getByClassName(cls) {
    var elesList = [];//准备一个空数组
    //获取页面的所有元素
    var oElements = document.getElementsByTagName("*");
    //遍历所有元素
    for (var i = 0; i < oElements.length; i++) {
        //每一个元素是否带有className属性，并且它的值是否等于‘box’
        var arrClass = oElements[i].className.split(" ");
        for (var j = 0; j < arrClass.length; j++) {
            if (arrClass[j] == cls) {
                elesList.push(oElements[i]);
                break;
            }
        }
    }
    return elesList;
}

//封装getStyle 获取元素样式，解决兼容问题
//dom为要获取元素的名称，atrr为要获取的属性值
//getComputedStyle(元素名称,"伪元素")[属性]
function getStyle(dom, attr) {
    if (dom.currentStyle) {//IE
        return dom.currentStyle[attr];
    } else {
        return getComputedStyle(dom, null)[attr];
    }
}
// console.log(getStyle(box1,'width'));

//获取下一个兄弟节点
function getNextNode(dom) {
    if (dom.nextElementSibling) {
        return dom.nextElementSibling;
    } else {
        return dom.nextSibling;
    }
}

//获取上一个兄弟节点
function getPrevNode(dom) {
    if (dom.previousElementSibling) {
        return dom.previousElementSibling;
    } else {
        return dom.previousSibling;
    }
}

//阻止事件冒泡，e为var e = evt||window.event;
function prevBubb(e) {
    if (e.stopPropagation) {
        return e.stopPropagation();
    } else {
        return e.cancelBubble = true;
    }
}


//添加事件监听，兼容低版本浏览器
//dom：给那个元素添加事件
//type:事件类型
//cb:（事件处理函数，你需要做的事）回调函数
function addevent(dom, type, cb) {
    if (dom.attachEvent) {
        addevent.attachEvent("on" + type, cb);
    } else {
        dom.addEventListener(type, cb);
    }
}
//移除监听事件
function removeEvent(dom, type, cbName) {
    if (dom.detachEvent) {
        dom.detachEvent('on' + type, cbName);
    }
}


//自己封装一个 insertAfter 将元素插入到某个元素的后面
//parent:父节点
//newElement 新节点
//refElement 
function insertAfter(parent, newElement, refElement) {
    //参照位置后一个元素
    var oNext = refElement.nextElementSibling || refElement.nextSibling
    parent.insertBefore(newElement, oNext);
}



//封装cookie的增删查改操作
//()()自执行函数，又叫自运行函数
//避免全局污染
const $$$ = (function () {
    return {
        //获取所有的cookie数据
        getCookieAll: function () {
            var result = {};//定义对象接收键和值
            //1、一条数据 age=20 ==>{age=20}
            //2、多条数据 age=20; sex=男; address=中国
            //                      {age:20,sex:男,address:中国}
            var strCookie = document.cookie;//获取所有的cookie
            var arr = strCookie.split("; ");//定义数组接收切割后的strCookie
            for (var i = 0; i < arr.length; i++) {
                var resList = arr[i].split("=");//定义数组接收切割后的arr
                var key = resList[0];
                var val = resList[1];
                result[key] = val;
            }
            return result;
        },
        //根据key获取值
        getCookie: function (key) {//key是age
            var obj = this.getCookieAll()//{age:20;sex:男}
            return obj[key];
        },
        setCookie: function (key, val, days, path, domain, secure) {
            //key=value;expires=xxx;path=xxx;domain=xxx;secure
            var strCookie = '';
            if (key) {
                strCookie += key + "=" + val + ";";
            }
            if (typeof (days) == "number") {
                var date = new Date();
                date.setDate(date.getDate() + days);
                strCookie += 'expires=' + date + ";";
            }
            if (path) {
                strCooike += "path=" + path + ";";
            }
            if (domain) {
                strCookie += "domain=" + domain + ";";
            }
            if (secure) {
                strCookie += "secure";
            }
            //把拼接好的字符串设置给cookie
            return document.cookie = strCookie;
        },
        //删除cookie
        removeCookie: function (key) {
            this.setCookie(key, '', -1);
        }
    }
})()

//封装  关于运动(轮播图)方法
const $$ = (function () {
    const getStyle = (obj, attr) => {
        if (window.getComputedStyle) {
            return window.getComputedStyle(obj, null)[attr]
        }
        return obj.currentStyle[attr];
    }

    const animate = (oEle, json, callback) => {
        //1.清空之前的定时器
        clearInterval(oEle.timer);
        //2.开始定时器
        oEle.timer = setInterval(function () {
            //3.定义一个标记默认为true
            let flag = true;
            //4.遍历json,
            for (var attr in json) {
                let current = 0;//当前的值
                let target = 0; //目标值
                let step = 0;   // 步长
                //5. 获取位置,目标位置
                if (attr === "opacity") { //放大100
                    current = parseFloat(getStyle(oEle, attr)) * 100;
                    target = parseFloat(json[attr]) * 100
                } else {
                    current = parseInt(getStyle(oEle, attr))
                    target = parseInt(json[attr])
                }
                //6. 计算 步长
                step = (target - current) / 10;
                step = step > 0 ? Math.ceil(step) : Math.floor(step);
                //7. 新的位置=当前位置+步长
                if (attr === "opacity") {
                    oEle.style.opacity = (current + step) / 100;
                    // ie 6 7 8透明?
                    oEle.style.filter = "alpha(opacity=" + (current + step) + ")"
                } else if (attr === "zIndex") {
                    oEle.style.zIndex = target;

                } else {
                    oEle.style[attr] = (current + step) + "px"
                }
                if (target != current) {
                    flag = false;
                }
            }
            if (flag) {
                //8.清空定时器 
                clearInterval(oEle.timer);
                if (typeof (callback) === "function") {
                    callback();//执行
                }
            }
        }, 30)
    }
    return {
        animate
    }
})()

//封装获取不可见区域的高度(滚动条)
//获取不可见区域的高度
// console.log(document.documentElement.scrollTop);
// console.log(document.body.scrollTop);
// console.log(window.pageYOffset);//ie9 +
//浏览器如何知道有没有声明头呢? DTD  document type define
// console.log(document.compatMode); 
//CSS1Compat  有   document.documentElement.scrollTop
//BackCompat  没    document.body.scrollTop
const getScroll = () => {
    if (window.pageYOffset != undefined) { //IE9+
        return {
            left: window.pageXOffset,
            top: window.pageYOffset
        }
    } else if (document.compatMode === "CSS1Compat") {//带有标准的声明头 DTD
        return {
            top: document.documentElement.scrollTop,
            left: document.documentElement.scrollLeft
        }
    }
    //没有DTD
    return {
        left: document.body.scrollLeft,
        top: document.body.scrollTop
    }
}

//封装一个方法判断是否是原型属性
//attr in obj只能判断有没有这个属性
//obj.hasOwnProperty(attr)只能判断是否是私有属性（构造函数的属性）
function isPublicProperty(obj, attr) {
    if (attr in obj && !obj.hasOwnProperty(attr)) {
        return true;
    }
    return false;
}